package com.hangzhou.welbeing.welbeinginstrument.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.PictureActivity;
import com.hangzhou.welbeing.welbeinginstrument.bean.NewPostBean;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemZanListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * 杭州惟君医疗科技有限公司15
 * Created by 20433 on 2018/2/1
 */

public class ReleaseHotAdapter extends  RecyclerView.Adapter<ReleaseHotAdapter.ViewHolder> implements View.OnClickListener {
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;
    private List<NewPostBean.DataBean> mlist = new ArrayList<>();
    private Context mcontext;
    private KyushuAdapter kyushuAdapter;

    public ReleaseHotAdapter(Context context) {
        this.mcontext = context;
    }
    private OnRecyclerViewItemZanListener mOnItemZanClickListener = null;
    public void setmOnItemZanClickListener(OnRecyclerViewItemZanListener mOnItemZanClickListener) {
        this.mOnItemZanClickListener = mOnItemZanClickListener;
    }
    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    public  void addItem(List<NewPostBean.DataBean> databean){
        mlist.clear();
        mlist.addAll(databean);
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //Log.e("tag","parseNetworkResponse==1111=onCreateViewHolder=====");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_release_recommended_circles_recyclerview,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int postion) {
       // Log.e("tag","parseNetworkResponse==1111=onBindViewHolder=====");
        Glide.with(mcontext).load("http://oop.welbeing.cn/Public/Uploads/" + mlist.get(postion).cus_logo).transform(new BitmapTransformation[]{new GlideCircleTransform(mcontext)}).placeholder(R.mipmap.head_portrait).error(R.mipmap.head_portrait).crossFade().into(viewHolder.iv_personal_head);
        final String cus_name = mlist.get(postion).cus_name;
        if(cus_name !=null && !TextUtils.isEmpty(cus_name)){
            viewHolder.tv_name.setText(cus_name);
        }
        String addtime = mlist.get(postion).addtime;
        if(addtime != null && !TextUtils.isEmpty(addtime)){
            viewHolder.tv_date.setText(addtime);
        }
        String po_name = mlist.get(postion).po_name;
        if(po_name != null && !TextUtils.isEmpty(po_name)){
            viewHolder.tv_titile.setText(po_name);
        }
        String content =  mlist.get(postion).content;
        if(content!= null && !TextUtils.isEmpty(content)){
            viewHolder.tv_content.setText(content);
        }
        int hits_like =  mlist.get(postion).hits_like;
        if(hits_like >= 0){
            viewHolder.tv_thumb_up.setText(mlist.get(postion).hits_like + "");
        }
        String count =  mlist.get(postion).count;
        if(content!= null && !TextUtils.isEmpty(content)){
            viewHolder.tv_commentaries.setText(count);
        }
        int lstate = mlist.get(postion).lstate;
        if(lstate == 0){
            viewHolder.iv_thumb_up.setImageResource(R.mipmap.zan_90);
        }else{
            viewHolder.iv_thumb_up.setImageResource(R.mipmap.zanz_00);
        }
        int state = mlist.get(postion).state;
        if(state == 0 ){
            viewHolder.tv_attention.setText("关注");
        }else {
            viewHolder.tv_attention.setText("已关注");
        }
       // viewHolder.iv_thumb_up.setImageResource(R.mipmap.zan);
        viewHolder.iv_thumb_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mOnItemZanClickListener != null){
                    mOnItemZanClickListener.onItemZanClick(v,postion,viewHolder.tv_thumb_up);
                }
            }
        });
        final String video = mlist.get(postion).video;
        String vpic = mlist.get(postion).vpic;
        viewHolder.tv_attention.setOnClickListener(new View.OnClickListener() { // TODO 要判断是否关注
            @Override
            public void onClick(View v) {
                if(mOnItemClickListener != null ){
                    mOnItemClickListener.onItemFirstButtonClick(v,postion);
                }
            }
        });
        if (video != null && !TextUtils.isEmpty(video)) {
            viewHolder.rl_video.setVisibility(View.VISIBLE);
            viewHolder.recyclerview.setVisibility(View.GONE);
            viewHolder.iv_jZVideoPlayerStandard.setUp(video, JZVideoPlayer.SCREEN_WINDOW_LIST,"");
            Glide.with(mcontext).load("http://oop.welbeing.cn/Public/Uploads/"+vpic).into(viewHolder.iv_jZVideoPlayerStandard.thumbImageView);
        } else {
            viewHolder.rl_video.setVisibility(View.GONE);
            viewHolder.recyclerview.setVisibility(View.VISIBLE);

            ArrayList<String> arrayList = new ArrayList();
            final NewPostBean.DataBean DataBean = this.mlist.get(postion);
            List<String> urlbean = DataBean.po_url;
            if (urlbean != null && urlbean.size() > 0) {
                for (String s : urlbean) {
                    arrayList.add(s);
                }
                GridLayoutManager    gridLayoutManager = new GridLayoutManager(this.mcontext, 3);
                gridLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
                viewHolder.recyclerview.setLayoutManager(gridLayoutManager);
                kyushuAdapter = new KyushuAdapter(this.mcontext, arrayList);
                viewHolder.recyclerview.setAdapter(kyushuAdapter);

                kyushuAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                    public void onItemClick(View paramAnonymousView) {
                        int postion_01 = viewHolder.recyclerview.getChildAdapterPosition(paramAnonymousView);
                        Intent intent = new Intent(mcontext, PictureActivity.class);
                        intent.putExtra("postion",postion_01);
                        intent.putExtra("po_id",DataBean.po_id);
                        intent.putExtra("po_name",DataBean.po_name);
                        intent.putExtra("addtime",DataBean.addtime);
                        intent.putExtra("video",DataBean.video);
                        intent.putExtra("vpic",DataBean.vpic);
                        intent.putExtra("content",DataBean.content);
                        intent.putExtra("cat_id",DataBean.cat_id);
                        intent.putExtra("cus_id",DataBean.cus_id);
                        intent.putExtra("cus_name",DataBean.cus_name);
                        intent.putExtra("cus_logo",DataBean.cus_logo);
                        intent.putExtra("hits_like",DataBean.hits_like);
                        intent.putExtra("lstate",DataBean.lstate);
                        intent.putExtra("col",DataBean.col);
                        intent.putExtra("state",DataBean.state);
                        intent.putExtra("count",DataBean.count);
                        intent.putExtra("lCount",DataBean.lCount);
                        intent.putExtra("po_url", (Serializable)DataBean.po_url);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        mcontext.startActivity(intent);
                    }

                    public void onItemFirstButtonClick(View paramAnonymousView, int paramAnonymousInt) {

                    }
                });
            }
        }


    }


    @Override
    public int getItemCount() {
        return mlist.size();
    }

    @Override
    public void onClick(View view) {
        if(mOnItemClickListener != null ){
            mOnItemClickListener.onItemClick(view);
        }
    }
    /**
     * 身体布局Holder
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_personal_head;
        private ImageView iv_start;
        private ImageView iv_thumb_up;
        private ImageView iv_user_picture_02;
        private ImageView iv_user_picture_03;
        private ImageView iv_video;
        private LinearLayout ll_user_picture;
        private RecyclerView recyclerview;
        private RelativeLayout rl_video;
        private TextView tv_classification;
        private TextView tv_commentaries;
        private TextView tv_attention;
        private TextView tv_content;
        private TextView tv_date;
        private TextView tv_name;
        private TextView tv_thumb_up;
        private TextView tv_titile;
        private JZVideoPlayerStandard iv_jZVideoPlayerStandard;
        public ViewHolder(View itemView) {
            super(itemView);
            this.tv_name = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_name);
            this.tv_date = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_date);
            this.tv_titile = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_titile);
            this.tv_content = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_content);
            this.tv_thumb_up = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_thumb_up);
            this.tv_commentaries = (TextView) itemView.findViewById(R.id.item_me_recyclerview_tv_commentaries);
            this.tv_attention = (TextView) itemView.findViewById(R.id.item_release_recommended_circles_recyclerview_tv_attention);
            this.iv_thumb_up = (ImageView) itemView.findViewById(R.id.item_me_recyclerview_iv_thumb_up);
            this.recyclerview = (RecyclerView) itemView.findViewById(R.id.item_me_recyclerview_recyclerview);
            this.rl_video = (RelativeLayout) itemView.findViewById(R.id.item_me_recyclerview_rl_video);
            this.iv_video = (ImageView) itemView.findViewById(R.id.item_me_recyclerview_iv_video);
            this.iv_start = (ImageView) itemView.findViewById(R.id.item_me_recyclerview_iv_start);
            this.iv_personal_head = (ImageView) itemView.findViewById(R.id.item_me_recyclerview_iv_personal_head);
            iv_jZVideoPlayerStandard = (JZVideoPlayerStandard) itemView.findViewById(R.id.item_me_recyclerview_iv_jZVideoPlayerStandard);

        }
    }
}
